package com.citywithincity.redis.models;

import java.util.Set;

import com.citywithincity.interfaces.IDestroyable;
import com.citywithincity.redis.utils.RedisUtil;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.Pipeline;

public class RedisZSetKeys implements IDestroyable {
	
	private String zsetKey;
	
	
	public RedisZSetKeys(String zsetKey) {
		this.zsetKey = zsetKey;
	}
	
	
	public Set<String> zrevrange(int count){
		Jedis jedis = null;
		try{
			jedis = RedisUtil.read();
			return jedis.zrevrange(zsetKey, 0, count-1);
		}finally{
			if(jedis!=null)jedis.close();
		}
	}
	
	public void add(Object id){
		Jedis jedis = null;
		try{
			jedis = RedisUtil.write();
			jedis.zincrby(zsetKey, 1, String.valueOf(id));
		}finally{
			if(jedis!=null)jedis.close();
		}
	}
	
	public void edit(Pipeline jedis, Object id, double score  ){
		jedis.zadd(zsetKey, score, String.valueOf(id));
	}
	
	public void edit(Object id, double score  ){
		Jedis jedis = null;
		try{
			jedis = RedisUtil.write();
			jedis.zadd(zsetKey, score, String.valueOf(id));
		}finally{
			if(jedis!=null)jedis.close();
		}
	}
	public void delete(Pipeline jedis,Object id){
		jedis.zrem(zsetKey, String.valueOf(id));
	}
	
	public void delete(Object id){
		Jedis jedis = null;
		try{
			jedis = RedisUtil.write();
			jedis.zrem(zsetKey, String.valueOf(id));
		}finally{
			if(jedis!=null)jedis.close();
		}
		
	}
	

	
	
	public boolean contains(Object id){
		Jedis jedis = null;
		try{
			jedis = RedisUtil.read();
			return jedis.zscore(zsetKey, String.valueOf(id))!=null;
		}finally{
			if(jedis!=null)jedis.close();
		}
	}
	
	
	public String getKey() {
		return zsetKey;
	}
	
	
	

	@Override
	public void destroy() {
		// TODO Auto-generated method stub
		
	}

}
